package demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description: 买牌->洗牌->发牌
 * User: 石方旭0
 * Date: 2022-04-28
 * Time: 12:46
 */

class PokerCard {
     private int Rank;//扑克牌上的数字
     private String suit;//扑克牌上的花色

    public PokerCard(int rank, String suit) {
        Rank = rank;
        this.suit = suit;
    }

    public int getRank() {
        return Rank;
    }

    public void setRank(int rank) {
        Rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "PokerCard{" +
                "Rank=" + Rank +
                ", suit='" + suit + '\'' +
                '}';
    }
}
public class TestPoker {
    //扑克牌的花色数组
    public static final String[] Suits= {"♠", "♥", "♣", "♦"};
    public static List<PokerCard> buyPokerCard() {
        //现有一副牌将它放进ArrayList中的数组里面
        List<PokerCard> cardList = new ArrayList<>();
        //一共要放入52张牌 数字为1-13 花色分别为 ♠  ♥  ♣  ♦
        for(int i =0;i<3;++i) {
            for(int j=1;j<=13;++j) {
                //设置一张牌 标记花色和数字
                PokerCard pokerCard =  new PokerCard(j,Suits[i]);
                cardList.add(pokerCard);
            }
        }
          return cardList;
    }

    private static void Swap(List<PokerCard> pokerCards,int i,int index) {
        /**
         * int tmp = array[i] tmp = i
         * array[i] = array[j] index =i;
         * array[j] = tmp
         */
        //采用面向对象来进行交换两个数字
        PokerCard tmp = pokerCards.get(i);//先得到i下标的扑克牌放到tmp里面
        pokerCards.set(i,pokerCards.get(index));//接着在吧这个i下标的扑克牌放入index下标上
        pokerCards.set(index,tmp);
    }
    public static void shuffle(List<PokerCard> pokerCards) {
        //也就是将扑克牌打乱
        for(int i = pokerCards.size()-1;i>0;--i) {
            Random random = new Random();//产生随机数
            int index = random.nextInt(i);//产生0-(i-1)之间的随机数
            Swap(pokerCards,i,index);
        }
    }

    public static void main(String[] args) {
        //买好扑克牌
         List<PokerCard> pokerCards = buyPokerCard();
        System.out.println(pokerCards);
        shuffle(pokerCards);
        System.out.println(pokerCards);
        //接下来开始发牌
        List<PokerCard> hands1 = new ArrayList<>();
        List<PokerCard> hands2 = new ArrayList<>();
        List<PokerCard> hands3 = new ArrayList<>();
        List<List<PokerCard>> hands = new ArrayList<>();
        hands.add(hands1);
        hands.add(hands2);
        hands.add(hands3);
        //每个人有5张牌，总共给3个人发牌
        for(int i =0;i<3;++i) {
            for (int j = 0; j < 5; j++) {
                PokerCard cards = pokerCards.remove(0);
                List<PokerCard> hand = hands.get(i);
                hand.add(j,cards);
            }
        }
        System.out.println("三人斗地主：->");
        System.out.println("裴浩森："+hands1);
        System.out.println("牛磊："+hands2);
        System.out.println("卢鑫喻："+hands3);
        System.out.println("剩下的牌"+pokerCards);
    }

}
